


//
//  MenuListView.m
//  条件选择
//
//  Created by Michael Li on 2018/7/16.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MenuListView.h"
#import "ChoseOneTabView.h"
#import "ChoseOneCollectionView.h"
#import "UIView+ZF.h"
#import "ChoseOneCell.h"
#import "ChoseOneCollectionViewCell.h"
#import "ChoseModel.h"
#import "ReleaseModel.h"
#define kSize self.frame.size
#define kS_W [UIScreen mainScreen].bounds.size.width
#define kS_H [UIScreen mainScreen].bounds.size.height
#define kLineW 0.5f
#define kLineH 25.f
#define kLineCor [UIColor colorWithWhite:0.850 alpha:1.000]

#define kRowH 44
#define kDefTitleCor [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]
//#define kSelTitleCor [UIColor colorWithRed:14/255.0 green:107/255.0 blue:255/255.0 alpha:1]
@interface MenuListView()<UITableViewDelegate, UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIView *shadow;
@property (nonatomic, strong) ChoseOneTabView *tabView;
@property (nonatomic, strong) ChoseOneCollectionView *collectionView;
@property(nonatomic,strong) MCButton *btn;
@property(nonatomic,strong) UIButton *preBtn;
@property(nonatomic,assign) NSInteger tagNum;
@property(nonatomic,assign) NSInteger tagBtn;
@property(nonatomic,strong) NSString *btnTitle;
@property (nonatomic, strong) NSMutableArray *oneDataSource;
@property (nonatomic, strong) NSMutableArray *twoDataSource;
@property (nonatomic, strong) NSMutableArray *threeDataSource;
@property(nonatomic,assign) NSInteger preOneRow;
@property(nonatomic,assign) NSInteger preTwoRow;
@property(nonatomic,assign) NSInteger preThreeRow;
@end
static NSString *identifierChoseOneCell = @"oneChoseOneCell";
static NSString *identifierChoseOneCollectionViewCell = @"ChoseOneCollectionViewCell";
@implementation MenuListView
-(NSMutableArray *)oneDataSource
{
    if (!_oneDataSource) {
        _oneDataSource = [NSMutableArray array];
        for (int i = 0;i< 50 ;i++) {
            if (i == 0) {
                [_oneDataSource addObject:@"1"];
            }else {
                [_oneDataSource addObject:@"0"];
                
            }
        }
    }
    return _oneDataSource;
}
-(NSMutableArray *)twoDataSource
{
    if (!_twoDataSource) {
        _twoDataSource = [NSMutableArray array];
        for (int i = 0;i< 50 ;i++) {
            if (i == 0) {
                [_twoDataSource addObject:@"1"];
            }else {
                
                [_twoDataSource addObject:@"0"];
            }
        }
    }
    return _twoDataSource;
}
-(NSMutableArray *)threeDataSource
{
    if (!_threeDataSource) {
        _threeDataSource = [NSMutableArray array];
        for (int i = 0;i< 100 ;i++) {
            if (i == 0) {
                [_threeDataSource addObject:@"1"];
            }else {
                [_threeDataSource addObject:@"0"];
            }
            
        }
    }
    return _threeDataSource;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.tagBtn = 100;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shadowView) name:@"shadowView" object:nil];
    }
    return self;
}
-(void)shadowView
{
    self.shadow.alpha = 0.f;
    self.tabView.h = 0;
    self.tabView.h = 0;
    self.tabView.h = 0;
    [self.shadow removeFromSuperview];
    [self.tabView removeFromSuperview];
    [self.collectionView removeFromSuperview];
    [self.collectionView removeFromSuperview];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)reloadCollectionViewData
{
    [self.collectionView reloadData];
}
- (void)reloadTableViewData
{
    [self.tabView reloadData];
}
-(void)setTitles:(NSArray *)titles
{
    XLog(@"titles:%@",titles);
    for (int i = 0; i < titles.count; i++) {
        CGFloat buttonW = (self.w - (titles.count-1)*kLineW)/titles.count;
        CGFloat buttonH = self.frame.size.height;
        CGFloat buttonX = (buttonW+kLineW) * i;
        CGRect btnFrame = CGRectMake(buttonX, 0.f, buttonW, buttonH);
        self.btn = [[MCButton alloc]initWithFrame:btnFrame];
        [self addSubview:self.btn];
        self.btn.buttonStyle = imageRight;
        UIImage *imageNor = [UIImage imageNamed:@"icon_160"];
        UIImage *imageHig = [UIImage imageNamed:@"icon_157"];
        [self.btn setImage:imageNor forState:UIControlStateNormal];
        [self.btn setImage:imageHig forState:UIControlStateSelected];
        [self.btn setTitle:titles[i] forState:UIControlStateNormal];
        [self.btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.btn.backgroundColor = [UIColor whiteColor];
        self.btn.tag = i;
        self.btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.btn setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1] forState:UIControlStateNormal];
        [self.btn setTitleColor:kDefTitleCor forState:UIControlStateNormal];
        [self.btn setTitleColor:kOrangeBg forState:UIControlStateSelected];
        // 按钮之间的竖直分割线
//        if (i < titles.count-1) {
//            CGFloat lineX = buttonX + buttonW;
//            CGFloat lineY = (self.h-kLineH)/2;
//            CGRect lineFrame = CGRectMake(lineX, lineY, kLineW, kLineH);
//            UIView *line = [[UIView alloc] initWithFrame:lineFrame];
//            line.backgroundColor = kLineCor;
//            [self addSubview:line];
//        }
    }
    // 下边的分割线
//    CGRect bottomLineFrame = CGRectMake(0, self.h-1.f, kS_W, 0.5f);
//    UIView *bottomLine = [[UIView alloc] initWithFrame:bottomLineFrame];
//    bottomLine.backgroundColor = kLineCor;
//    [self addSubview:bottomLine];
    
}
-(NSArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSArray array];
    }
    return _dataSource;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowe = [[UICollectionViewFlowLayout alloc]init];
        CGRect frame = CGRectMake(0.f, self.navigationHeight + self.h, self.w, 0.f);
        //XLog(@"navigationHeight:%ld",(long)self.navigationHeight);
        flowe.minimumLineSpacing = 0;
        flowe.minimumInteritemSpacing = 0;
        _collectionView = [[ChoseOneCollectionView alloc] initWithFrame:frame collectionViewLayout:flowe];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
         [_collectionView registerClass:[ChoseOneCollectionViewCell class] forCellWithReuseIdentifier:identifierChoseOneCollectionViewCell];
        
    }
    return _collectionView;
}
- (UITableView *)tabView
{
    if (!_tabView) {
        CGRect frame = CGRectMake(0.f, self.navigationHeight + self.h, self.w, 0.f);
        XLog(@"self.navigationHeight + self.h:%f",(long)self.navigationHeight + self.h);
        _tabView = [[ChoseOneTabView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _tabView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.backgroundColor = [UIColor whiteColor];
        [_tabView registerClass:[ChoseOneCell class] forCellReuseIdentifier:identifierChoseOneCell];
    }
    return _tabView;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //XLog(@"dataSource:%ld",self.dataSource.count);
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChoseOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierChoseOneCollectionViewCell forIndexPath:indexPath];
    ReleaseModel *mode = self.dataSource[indexPath.row];
    cell.label.text = mode.content;
    
    //XLog(@"cell.label.text:%@",cell.label.text);
    NSString *num = nil;
    
    if (self.preBtn.tag == 0) {
        num = nil;
        num = self.oneDataSource[indexPath.row];
        //XLog(@"dataSourceOne:%@",_oneDataSource);
        if ([num integerValue] == 1) {
            cell.label.textColor = [UIColor colorWithRed:47/255.0 green:119/255.0 blue:246/255.0 alpha:1];
            cell.label.layer.borderColor = [UIColor colorWithRed:47/255.0 green:119/255.0 blue:246/255.0 alpha:1].CGColor;

        }else {
            cell.label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
            cell.label.layer.borderColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1].CGColor;

        }
    }else if (self.preBtn.tag == 1) {
        //        //XLog(@"num:%@",num);
        //
        num = nil;
        //        //XLog(@"self.twoDataSource:%@",self.twoDataSource);
        num = self.twoDataSource[indexPath.row];
        if ([num integerValue] == 1) {
            cell.label.textColor = [UIColor colorWithRed:47/255.0 green:119/255.0 blue:246/255.0 alpha:1];
            cell.label.layer.borderColor = [UIColor colorWithRed:47/255.0 green:119/255.0 blue:246/255.0 alpha:1].CGColor;

        }else {
            cell.label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
            cell.label.layer.borderColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1].CGColor;
        }
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kUIScreen.size.width/3, kRowH);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [UIView animateWithDuration:0.2f animations:^{
        self.shadow.alpha = 0.f;
        self.collectionView.h = 0.f;
    } completion:^(BOOL finished) {
        [self.shadow removeFromSuperview];
        [self.collectionView removeFromSuperview];
    }];
    
    for (id oc in self.subviews) {
        if ([oc isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)oc;
            XLog(@"btn:%@",btn);
            XLog(@"currentTitle:%@",[btn currentTitle]);
//            UIImage *imageNor = [UIImage imageNamed:@"icon_160"];
//            UIImage *imageHig = [UIImage imageNamed:@"icon_157"];
            if (btn.tag == self.tagNum){
                [btn setTitle:self.dataSource[indexPath.row] forState:UIControlStateNormal];
                btn.selected = NO;
                self.btnTitle  = [btn currentTitle];
                [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                self.tagBtn = btn.tag;
//                [btn setImage:imageHig forState:UIControlStateNormal];
            }else {
                 [btn setTitleColor:kDefTitleCor forState:UIControlStateNormal];
//                [btn setImage:imageNor forState:UIControlStateNormal];
                btn.selected = NO;
            }
        }
    }
//    NSInteger tag = 0;
//        tag = self.preBtn.tag;
//    XLog(@"self.preBtn.tag:%ld",self.preBtn.tag);
//        if (tag == 0) {
//            [self.oneDataSource replaceObjectAtIndex:indexPath.row withObject:@"1"];
//            if (self.preOneRow != indexPath.row) {
//                [self.oneDataSource replaceObjectAtIndex:self.preOneRow withObject:@"0"];
//            }
//            self.preOneRow = indexPath.row;
//        }else if (tag == 1) {
//            [self.twoDataSource replaceObjectAtIndex:indexPath.row withObject:@"1"];
//            if (self.preTwoRow != indexPath.row) {
//                [self.twoDataSource replaceObjectAtIndex:self.preTwoRow withObject:@"0"];
//            }
//            self.preTwoRow = indexPath.row;
//        }
//    if (self.clickListView) {
//        self.clickListView(indexPath.row, nil);
//    };
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kRowH;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChoseOneCell *cell = [ChoseOneCell cellWithTableView:tableView];
    ReleaseModel *mode = self.dataSource[indexPath.row];
    cell.title = mode.content;
    XLog(@"mode.content:%@",mode.content);
    XLog(@"self.dataSource:%@",self.dataSource);
//    NSInteger num = nil;
//    num = self.dataSource[indexPath.row];
    if (indexPath.row == 0) {
        cell.titleLabel.textColor = [UIColor colorWithRed:14/255.0 green:107/255.0 blue:255/255.0 alpha:1];
        cell.imgView.image = [UIImage imageNamed:@"ic_news_selected"];
    }else {
        cell.titleLabel.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        cell.imgView.image = nil;
        
        
    }
    return cell;
}

- (void)resetStatus:(UIButton *)button
{
    for (id oc in self.subviews) {
        if ([oc isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)oc;
            btn.selected = NO;
            UIImage *imageNor = [UIImage imageNamed:@"ic_home_wallet_date_datemore"];
            UIImage *imageHig = [UIImage imageNamed:@"ic_news_up-1"];
            if (btn.tag == self.tagBtn) {
                [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                [btn setImage:imageHig forState:UIControlStateNormal];
            }else {
                [btn setTitleColor:kDefTitleCor forState:UIControlStateNormal];
                [btn setImage:imageNor forState:UIControlStateNormal];
            }
            
        }

     }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView animateWithDuration:0.2f animations:^{
        self.shadow.alpha = 0.f;
        self.tabView.h = 0.f;
    } completion:^(BOOL finished) {
        [self.shadow removeFromSuperview];
        [self.tabView removeFromSuperview];
    }];

    for (id oc in self.subviews) {
        if ([oc isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)oc;
            UIImage *imageNor = [UIImage imageNamed:@"ic_home_wallet_date_datemore"];
            UIImage *imageHig = [UIImage imageNamed:@"ic_news_up-1"];
            if (btn.tag == self.tagNum){
                [btn setTitle:self.dataSource[indexPath.row] forState:UIControlStateNormal];
                btn.titleLabel.textColor = kOrangeBg;
                btn.selected = NO;
                self.btnTitle  = [btn currentTitle];
                [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                self.tagBtn = btn.tag;
                [btn setImage:imageHig forState:UIControlStateNormal];
            }else {
                btn.titleLabel.textColor = kDefTitleCor;
                [btn setImage:imageNor forState:UIControlStateNormal];
                btn.selected = NO;
            }
        }
    }
    
    if (self.clickListView) {
        self.clickListView(indexPath.row, self.dataSource[indexPath.row]);
        [self.threeDataSource replaceObjectAtIndex:indexPath.row withObject:@"1"];
        if (self.preThreeRow != indexPath.row) {
            [self.threeDataSource replaceObjectAtIndex:self.preThreeRow withObject:@"0"];
        }
        self.preThreeRow = indexPath.row;
    }
}

-(void)clickBtn:(UIButton *)btn
{
    btn.selected = !btn.selected;
    UIView *superView = self.superview;
    [superView.window addSubview:self.shadow];
    [superView.window addSubview:self.tabView];
    [superView.window addSubview:self.collectionView];
    if (self.returnValueBlock) {
        self.returnValueBlock(btn, btn.tag, btn.selected);
    }
//    UIImage *imageNor = [UIImage imageNamed:@"icon_160"];
//    UIImage *imageHig = [UIImage imageNamed:@"icon_157"];
    if (self.preBtn != btn) {
        self.preBtn.selected = NO;
        [self.preBtn setTitleColor:kDefTitleCor forState:UIControlStateNormal];
//        [self.preBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//        [self.preBtn setImage:imageNor forState:UIControlStateNormal];
    }else {
        [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
//        [btn setImage:imageHig forState:UIControlStateNormal];
    }
    if (btn.selected) {
        self.tagNum = btn.tag;
        if (btn.tag == 0) {
//            _dataSource = _dataSourceOne;
            //XLog(@"_oneDataSource:%@",_oneDataSource);
//            [self showListViewAnimation];
            _collectionView.hidden = NO;
            _tabView.hidden = YES;
        }else {
//            _dataSource = _dataSourceThree;
//            [self showListViewAnimation];
//            _collectionView.hidden = YES;
//            _tabView.hidden = NO;
            _collectionView.hidden = YES;
            _tabView.hidden = NO;
        }
//        [self.collectionView reloadData];
//        [self.tabView reloadData];
    }else {
        [self hideListViewAnimation];
    }
    self.preBtn = btn;
//

}
- (void)setNavigationHeight:(NSInteger)navigationHeight
{
    _navigationHeight = navigationHeight;
    
}
- (UIView *)shadow
{
    if (!_shadow) {
        _shadow = [[UIView alloc] init];
        _shadow.alpha = 0.f;
        _shadow.userInteractionEnabled = YES;
        _shadow.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
        _shadow.frame = CGRectMake(0, self.navigationHeight + self.h, kS_W, kS_H);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shadowViewClick)];
        [_shadow addGestureRecognizer:tap];
    }
    return _shadow;
}

#pragma mark - 点击背景
- (void)shadowViewClick
{
    [self hideListViewAnimation];
    [self resetStatus:self.btn];
}
// 隐藏下拉列表
- (void)hideListViewAnimation
{
    [UIView animateWithDuration:0 animations:^{
        self.shadow.alpha = 0.f;
        self.tabView.h = 0.f;
        self.collectionView.h = 0.f;
    } completion:^(BOOL finished) {
        [self.shadow removeFromSuperview];
        [self.tabView removeFromSuperview];
        [self.collectionView removeFromSuperview];
        [self resetStatus:self.btn];
    }];

}
// 更具数据源里边数据情况，返回TableView高度
- (CGFloat)maxCollectionListHeightWithModel:(NSArray *)dataSource
{
    CGFloat height = (dataSource.count /3+1)*kRowH;
    return height;
}
// 更具数据源里边数据情况，返回TableView高度
- (CGFloat)maxListHeightWithModel:(NSArray *)dataSource
{
    
    CGFloat height = dataSource.count*kRowH;
    return height;
}
// 显示下拉列表
- (void)showListViewAnimation
{
    

    CGFloat height = [self maxListHeightWithModel:self.dataSource];
    CGFloat maxCollectionListHeightWithModel = [self maxCollectionListHeightWithModel:self.dataSource];
    [UIView animateWithDuration:0.3 animations:^{
        self.shadow.alpha = 1.f;
        self.tabView.h = height;
        self.collectionView.h = maxCollectionListHeightWithModel;
    }];
    
    for (id oc in self.subviews) {
        if ([oc isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)oc;
            UIImage *imageNor = [UIImage imageNamed:@"ic_home_wallet_date_datemore"];
            UIImage *imageHig = [UIImage imageNamed:@"ic_news_up-1"];
            if (btn.tag == self.tagNum) {
                [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                [btn setImage:imageHig forState:UIControlStateNormal];
            }else {
                [btn setTitleColor:kDefTitleCor forState:UIControlStateNormal];
                [btn setImage:imageNor forState:UIControlStateNormal];
            }
            
        }
    }
}
@end
